/**
 * 枚举类
 */
import ModelingClass from './ModelingClass.js';
import EnumSubTypeCaption from './EnumSubTypeCaption.js';
import EnumConstant from './EnumConstant.js';
import TypeKind from '../enums/TypeKind.js';

export default class EnumClass extends ModelingClass {

    /**
     * 是否简单枚举类型，简单枚举类型以简单名称作为类型名
     * @type {boolean}
     */
    simple = false;
    /**
     * @type {[EnumSubTypeCaption]}
     */
    subCaptions = [];
    /**
     * @type {[EnumConstant]}
     */
    constants = [];

    constructor(packageName, simpleName) {
        super(TypeKind.ENUM, packageName, simpleName);
    }

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let clazz = new EnumClass(object.packageName, object.simpleName);
        clazz.update(object);
        return clazz;
    }

    update(object) {
        super.update(object);
        if (typeof object === 'object') {
            if (object.subCaptions) {
                this.subCaptions = object.subCaptions.map(subCaption => {
                    if (subCaption instanceof EnumSubTypeCaption) {
                        return subCaption.clone();
                    } else {
                        return new EnumSubTypeCaption().update(subCaption);
                    }
                });
            }
            if (object.constants) {
                this.constants = object.constants.map(constant => {
                    if (constant instanceof EnumConstant) {
                        return constant.clone();
                    } else {
                        return new EnumConstant().update(constant);
                    }
                });
            }
        }
        return this;
    }

    clone() {
        return EnumClass.of(this);
    }

}
